package gbench.appdemo.jdbcapp.gsx;

import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.*;

import org.junit.jupiter.api.Test;
public class JunitConcurrent {
    
    static Long num = 0l;
    
    @Test
    public void foo() {// 触发竞争条件
        Stream.iterate(0, i->i+1).parallel().limit(1000000).forEach(i->{
            num++;
        });
        System.out.println(num);
    }
    
    @Test
    public void bar() {
        Stream.iterate(0, i->i+1).parallel().limit(1000000).forEach(i->{
            synchronized(this) {
                num++;
            }
        });
        System.out.println(num);
    }
    
    @Test
    public void qux() {
        var lock = new ReentrantLock();
        Stream.iterate(0, i->i+1).parallel().limit(1000000).forEach(i->{
            lock.lock();
            num++;
            lock.unlock();
        });
        System.out.println(num);
    }

}
